"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 二级分区
Case Name   : range_range二级分区表：index_parameters(primary key)/表约束(约束推迟)
Create At   : 2022/2/24
Owner       : opentestcase012
Description :
    1.创建二级分区表，普通列指定primary key
    2.查看索引
    3.插入正确数据
    4.插入违反约束的数据
    5.创建二级分区表，指定表约束primary key
    6.开启事务并设置约束在事务提交时检查,插入重复数据后commit,查询数据
    7.开启事务并设置约束在事务提交时检查,插入重复数据后rollback,查询数据
    8.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.合理报错
    5.成功
    6.插入重复数据成功,commit合理报错,查询无数据
    7.插入重复数据成功,rollback后查询无数据
    8.清理成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Subpartition(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_subpartition_0261'
        self.t_name_01 = 't_subpartition_0261_01'
        self.i_name_01 = 'i_subpartition_0261_01'
        self.i_name_02 = 'i_subpartition_0261_02'

    def test_subpartition(self):
        text = '-----step1:创建二级分区表，普通列指定primary key expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table {self.t_name} (col_1 int, col_2 int not null , " \
            f"col_3 int not null , col_4 int primary key)" \
            f"partition by range (col_1) subpartition by range (col_2) " \
            f"( partition p_range_1 values less than(10) " \
            f"  ( subpartition p_range_1_1 values less than(5)," \
            f"    subpartition p_range_1_2 values less than(maxvalue)), " \
            f"  partition p_range_2 values less than(20) " \
            f"  ( subpartition p_range_2_1 values less than(5), " \
            f"    subpartition p_range_2_2 values less than(maxvalue)) " \
            f") enable row movement; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step2:查看索引 expect:成功-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\di')
        self.log.info(msg)
        self.assertIn(f'{self.t_name}_pkey', msg, '执行失败:' + text)

        text = '-----step3:插入正确数据 expect:成功-----'
        self.log.info(text)
        sql = f"insert into {self.t_name} values(1,1,1,1),(5,5,5,5);"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step4:插入违反约束的数据 expect:合理报错-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn('violates unique constraint', msg, '执行失败:' + text)

        text = '-----step5:创建二级分区表，指定表约束primary key expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table {self.t_name} (col_1 int, col_2 int not null , " \
            f"col_3 int not null , col_4 int ," \
            f"primary key (col_1,col_2) deferrable)" \
            f"partition by range (col_1) subpartition by range (col_2) " \
            f"( partition p_range_1 values less than(10) " \
            f"  ( subpartition p_range_1_1 values less than(5)," \
            f"    subpartition p_range_1_2 values less than(maxvalue)), " \
            f"  partition p_range_2 values less than(20) " \
            f"  ( subpartition p_range_2_1 values less than(5), " \
            f"    subpartition p_range_2_2 values less than(maxvalue)) " \
            f") enable row movement; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step6:开启事务并设置约束在事务提交时检查,插入重复数据后commit,查询数据 ' \
               'expect:插入重复数据成功,commit合理报错,查询无数据-----'
        self.log.info(text)
        sql = "start transaction;" \
            f"set constraints all deferred;" \
            f"insert into {self.t_name} values(1,1,1,1),(5,5,5,5);" \
            f"insert into {self.t_name} values(1,1,1,1),(5,5,5,5);" \
            f"commit;" \
            f"select * from {self.t_name};"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.constant.START_TRANSACTION_SUCCESS_MSG, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.SET_SUCCESS_MSG, msg, '执行失败:' + text)
        self.assertEqual(2, msg.count(self.constant.INSERT_SUCCESS_MSG),
                         '执行失败:' + text)
        self.assertIn(self.constant.unique_index_error_info, msg,
                      '执行失败:' + text)
        self.assertIn('0 row', msg, '执行失败:' + text)

        text = '-----step7:开启事务并设置约束在事务提交时检查,插入重复数据后rollback,查询数据 ' \
               'expect:插入重复数据成功,rollback后查询无数据-----'
        self.log.info(text)
        sql = "start transaction;" \
            f"set constraints all deferred;" \
            f"insert into {self.t_name} values(1,1,1,1),(5,5,5,5);" \
            f"insert into {self.t_name} values(1,1,1,1),(5,5,5,5);" \
            f"rollback;" \
            f"select * from {self.t_name};"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.constant.START_TRANSACTION_SUCCESS_MSG, msg,
                      '执行失败:' + text)
        self.assertIn(self.constant.SET_SUCCESS_MSG, msg, '执行失败:' + text)
        self.assertEqual(2, msg.count(self.constant.INSERT_SUCCESS_MSG),
                         '执行失败:' + text)
        self.assertIn(self.constant.ROLLBACK_MSG, msg, '执行失败:' + text)
        self.assertIn('0 row', msg, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----step8:清理环境-----')
        text_1 = '-----删除表 expect:成功-----'
        self.log.info(text_1)
        del_cmd = f'drop table if exists {self.t_name};' \
            f'drop table if exists {self.t_name_01};'
        msg = self.pri_sh.execut_db_sql(del_cmd)
        self.log.info(msg)
        self.assertEqual(2, msg.count(self.constant.TABLE_DROP_SUCCESS),
                         '执行失败:' + text_1)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
